import baseUtil from "../../utils/base_util";
import api from "../../utils/api_util";
import tips from "../../utils/tip";

const app = getApp()
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        info:'',
        switchCheck:"",
        //修改用户信息字段
        email:'',
        nikename:'',
        birthday:'',
        gender:'',
        modalName:'',
        inputChangeValue:'',
        modaltype:'',
    },
    onShow: function (options) {

        this.getUserData();
    },
    //性别
    sexChange:function(e){
        if(e.detail.value){

            this.data.gender = 'male';
        }else {
            this.data.gender = 'female';
        }

        this.setData({
            gender: this.data.gender
        })
        this.updateInfoData();
    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value);

        this.setData({
            birthday: e.detail.value
        })

        this.updateInfoData();
    },
    //弹出修改信息弹窗
    showModal: function (e) {
        let modalName = e.currentTarget.dataset.modal;
        this.data.modaltype = e.currentTarget.dataset.type;

        this.setData({
            modalName:modalName,
            modaltype:this.data.modaltype
        })
    },
    infoChangeInput:function(e){
        console.log('inputChangeValue:',e.detail.value)
         this.setData({
             inputChangeValue:e.detail.value
         })
    },
    //关闭弹窗
    closeModal: function (e) {
        this.setData({
            modalName: null
        })
    },
    //确定弹窗
    yesModal: function (e) {
        if(this.data.modaltype == 'name'){
            this.data.nikename = this.data.inputChangeValue;
        }else if(this.data.modaltype == 'email'){
            this.data.email =  this.data.inputChangeValue;
        }

        this.setData({
            modalName: null,
            email: this.data.email,
            nikename: this.data.nikename,
        })

        this.updateInfoData();
    },
    //获取用户信息
    getUserData: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.userInfo, {
            method: 'GET',
            data: {
                v: '1.0.0',
            },
            success: function (res) {
                if(res.data.gender == 'male'){
                    that.data.switchCheck = true;
                }else {
                    that.data.switchCheck = false;

                }
                that.setData({
                    info: res.data,
                    birthday: res.data.birthday,
                    gender: res.data.gender,
                    email: res.data.email,
                    switchCheck: that.data.switchCheck,
                })

                console.log('email:',that.data.email)
                console.log('switchCheck:',that.data.switchCheck)
            },
            headerInfo: function (res) {
                baseUtil.setStorageSync('token', res.token);
            }
        })
    },
    //修改用户信息
    updateInfoData: function () {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.updateInfo, {
            method: 'POST',
            data: {
                email:that.data.email,
                nickname:that.data.nikename,
                birthday:that.data.birthday,
                gender:that.data.gender,
                v: '1.0.0'
            },
            success: function (res) {
                  tips.alert(res.data);
                  that.getUserData();
            },
        })
    },
})